//依赖于jquery $.param
var request = {
	//ajax
	/*
	method	请求方法
	url 	请求地址
	headers	请求头对象{token,Authorization}
	data	字符串（查询字符串、json字符串）
	*/
	ajax:function(method,url,headers,data,handler){
		var xhr = new XMLHttpRequest();
		//打开请求
		xhr.open(method,url);
		//设置请求头
		for(var k in headers){
			xhr.setRequestHeader(k,headers[k]);
		}
		//设置请求体
		xhr.send(data)
		xhr.onreadystatechange = function(){
			if (xhr.readyState === 4) {
				if (xhr.status === 200) {
					let resp = JSON.parse(xhr.response)
					handler.call(xhr,resp);
				}
			}
		}
		
	},
	//get请求
	get:function(url,data,handler){
		url += "?"+$.param(data)
		return this.ajax("GET",url,{
			Authorization:token
		},null,handler);
	},

	//post请求 JSON
	postJOSN:function(url,data,handler){
		return this.ajax("POST",url,{
			Authorization:token,
			"Content-Type":"application/json"
		},JSON.stringify(data),handler);
	},
	//post请求 qs
	postQS:function(url,data,handler){
		return this.ajax("POST",url,{
			Authorization:token,
			"Content-Type":"application/x-www-form-urlencoded"
		},$.param(data),handler);
	}
}